#####################################
##        PEinjector/hook          ##
#####################################

# This moudle will wait PE loaded all
#     things.

# load moudles
import sys
import os
import time
import pickle
sys.path.append("X:\\PEinjector\\src")
sys.path.append("X:\\PEinjector\\tool")
try:
    os.chdir("X:\\PEinjector\\src")
except:
    sys.path.append(".")
    sys.path.append("..\\tool")
    sys.path.append("src")
    sys.path.append("tool")

log = __import__("log")
config = __import__("config")
action = __import__("action")
utils = __import__("utils")

# logo
logo = """\033[1m\033[34m
    ___  _____      __        __          
   / _ \\/ __(_)__  /_/__ ____/ /____  ____
  / ___/ _// / _ \\/ / -_) __/ __/ _ \\/ __/
 /_/  /___/_/_//_/ /\\__/\\__/\\__/\\___/_/   
              |___/                        
 _________________________________________
\033[0m\033[?25l"""
print(logo)

log.init()
log.info("hook start")

sys.stdout.write("\033[3A \033[37m")
for j in range(5):
    for i in range(41):
        sys.stdout.write(" "+"_"*(i+1)+"\n\033[1A")
        time.sleep(0.02)
    sys.stdout.write("\n\033[1A \033[34m")
    for i in range(41):
        sys.stdout.write(" "+"_"*(i+1)+"\n\033[1A")
        time.sleep(0.02)
    sys.stdout.write("\n\033[1A \033[37m")
sys.stdout.write("\033[0m\033[?25h\n\n\n")

sysdrive = os.getenv("SystemDrive")

username = "Default"
for i in os.listdir(f"{sysdrive}\\Users"):
    if i not in ("Default", "Default User", "Public", "All Users") and not os.path.isfile(f"{sysdrive}\\Users\\{i}"):
        username = i

# wait
while not os.path.exists(f"{sysdrive}\\Users\\{username}\\Desktop"): time.sleep(1)

# load action list

log.info("load action \"onload\"")
with open(config.TEMP_DIR.format(TEMP = "X:\\PEinjector") + "\\PEinjector.tmp", "rb") as file:
    lists = pickle.load(file)
alog = action.do_action(lists)
with open(config.ACTIONLOGPATH.format(DISK = utils.find_disk()), "w") as file:
    file.write(alog)
log.info("done")
